package com.redsoil.house.ui.remit.presenter;

import com.redsoil.house.app.AppContext;
import com.redsoil.house.base.RxPresenter;
import com.redsoil.house.model.DataManager;
import com.redsoil.house.model.bean.DictListVO;
import com.redsoil.house.model.bean.ReceiptListVO;
import com.redsoil.house.model.bean.ReceiptTotalVO;
import com.redsoil.house.model.http.exception.ApiException;
import com.redsoil.house.model.http.response.HttpResponse;
import com.redsoil.house.ui.remit.contract.RemitContract;
import com.redsoil.house.utils.RxUtil;
import com.redsoil.house.widget.CommonSubscriber;
import com.weidingqiang.rxfiflibrary2.utils.LogUtil;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：weidingqiang on 2017/9/7 15:39
 * 邮箱：dqwei@iflytek.com
 */

public class RemitPresenter extends RxPresenter<RemitContract.View> implements RemitContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public RemitPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(RemitContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    private void registerEvent(){

    }

    public void postReceipt(String year){
        Flowable.concat(mDataManager.postReceiptList(AppContext.getInstance().getToken(),AppContext.getInstance().getCurrentMoneyType(),year),
                mDataManager.postReceiptTotal(AppContext.getInstance().getToken(),AppContext.getInstance().getCurrentMoneyType(),year))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(
                        new CommonSubscriber<Object>(mView) {
                            @Override
                            public void onNext(Object data) {
//                                LogUtil.debug("","");
//                                ((HttpResponse) data).data
                                HttpResponse httpResponse = ((HttpResponse) data);


                                if(httpResponse.getData() instanceof ReceiptListVO){
                                    mView.postReceiptListSuccess((ReceiptListVO)(httpResponse.getData()));

                                }else if(httpResponse.getData() instanceof ReceiptTotalVO){
                                    mView.postReceiptTotalSuccess((ReceiptTotalVO)(httpResponse.getData()));
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                            @Override
                            public void onComplete() {
                                super.onComplete();
                                mView.postComplete();
                            }
                        }
                );
    }

}
